package dao.impl;

import dao.CompanyDao;
import model.Company;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * The type Company dao.
 *
 * @author gzp
 * @create 2022 -12-12 10:04
 */
public class CompanyDaoImpl implements CompanyDao {
    /**
     * The Pstmt.
     */
    PreparedStatement pstmt = null;
    /**
     * The Rs.
     */
    ResultSet rs = null;

    @Override
    public Company selectById(long cid) {
        Company company = null;
        String sql = "SELECT * FROM company WHERE cid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, cid);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                company = new Company();
                company.setCid(rs.getLong("cid"));
                company.setCname(rs.getString("cname"));
                company.setCemail(rs.getString("cemail"));
                company.setLogo(rs.getString("logo"));
                company.setCphone(rs.getString("cphone"));
                company.setDesc(rs.getString("desc"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return company;
    }

    @Override
    public Company login(String cname, String cpassword) {
        Company company = null;
        String sql = "SELECT * FROM company WHERE cname=? AND cpassword=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, cname);
            pstmt.setString(2, cpassword);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                company = new Company();
                company.setCname(rs.getString("cname"));
                company.setCpassword(rs.getString("cpassword"));
                company.setCemail(rs.getString("cemail"));
                company.setLogo(rs.getString("logo"));
                company.setCphone(rs.getString("cphone"));
                company.setDesc(rs.getString("desc"));
                company.setCid(rs.getInt("cid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return company;
    }

    @Override
    public int update(Company company) {
        int rows = 0;
        String sql = "UPDATE company SET cname=?,cpassword=?,cemail=?,logo=?,cphone=?,desc=? WHERE cid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, company.getCname());
            pstmt.setString(2, company.getCpassword());
            pstmt.setString(3, company.getCemail());
            pstmt.setString(4, company.getLogo());
            pstmt.setString(5, company.getCphone());
            pstmt.setString(6, company.getDesc());
            pstmt.setLong(7, company.getCid());
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public int insert(Company company) {
        int rows = 0;
        String sql = "INSERT INTO company(cname,cpassword,cemail,logo,cphone,`desc`) VALUES(?,?,?,?,?,?)";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, company.getCname());
            pstmt.setString(2, company.getCpassword());
            pstmt.setString(3, company.getCemail());
            pstmt.setString(4, company.getLogo());
            pstmt.setString(5, company.getCphone());
            pstmt.setString(6, company.getDesc());
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public List<Company> selectAll(int pageIndex, int pageSize) {
        List<Company> companyList = new ArrayList<Company>();
        String sql = "SELECT * FROM company LIMIT ?,?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, (pageIndex - 1) * pageSize);
            pstmt.setInt(2, pageSize);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Company company = new Company();
                company.setCid(rs.getLong("cid"));
                company.setCname(rs.getString("cname"));
                company.setCpassword(rs.getString("cpassword"));
                company.setCemail(rs.getString("cemail"));
                company.setLogo(rs.getString("logo"));
                company.setCphone(rs.getString("cphone"));
                company.setDesc(rs.getString("desc"));
                companyList.add(company);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return companyList;
    }

    @Override
    public List<Company> selectByName(String cname, int pageIndex, int pageSize) {
        List<Company> companyList = new ArrayList<Company>();
        String sql = "SELECT * FROM company where cname Like ? LIMIT ?,?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "%" + cname + "%");
            pstmt.setInt(2, (pageIndex - 1) * pageSize);
            pstmt.setInt(3, pageSize);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Company company = new Company();
                company.setCid(rs.getLong("cid"));
                company.setCname(rs.getString("cname"));
                company.setCpassword(rs.getString("cpassword"));
                company.setCemail(rs.getString("cemail"));
                company.setLogo(rs.getString("logo"));
                company.setCphone(rs.getString("cphone"));
                company.setDesc(rs.getString("desc"));
                companyList.add(company);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return companyList;
    }

    @Override
    public int countAll() {
        int count = 0;
        String sql = "SELECT count(*) FROM company";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return count;
    }

    @Override
    public int countByName(String cname) {
        int count = 0;
        String sql = "SELECT count(*) FROM company where cname like ? ";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "%" + cname + "%");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return count;
    }
}
